package com.goods.controller.business;

import com.github.pagehelper.Page;
import com.goods.business.service.SupplierService;
import com.goods.common.annotation.ControllerEndpoint;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * description:
 * create by sxy 2021/6/8
 */
@Api(tags = "business模块-物资来源相关接口")
@RestController
@RequestMapping("/business/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    //物资来源列表
    @ApiOperation(value = "物资来源列表", notes = "模糊查询用户来源列表")
    @GetMapping("/findSupplierList")
    public ResponseBean<PageVO<SupplierVO>> findSupplierList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                             SupplierVO supplierVO) {
        PageVO<SupplierVO> supplierList = supplierService.findSupplierList(pageNum, pageSize, supplierVO);
        return ResponseBean.success(supplierList);
    }

    //添加物资来源信息
    @ControllerEndpoint(exceptionMessage = "添加物资来源失败", operation = "添加物资来源")
    @ApiOperation(value = "添加物资", notes = "添加物资来源信息")
    @RequiresPermissions({"supplier:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated SupplierVO supplierVO) throws SystemException {
        supplierService.add(supplierVO);
        return ResponseBean.success();
    }

    //编辑物资来源信息之回显
    @GetMapping("edit/{id}")
    public ResponseBean<SupplierVO> edit(@PathVariable Long id) {
        SupplierVO supplierVO = supplierService.edit(id);
        return ResponseBean.success(supplierVO);
    }

    //修改物资来源信息
    @PutMapping("update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody SupplierVO supplierVO) throws SystemException {
        supplierService.update(id, supplierVO);
        return ResponseBean.success();
    }

    //删除物资来源信息
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id) throws SystemException {
        supplierService.delete(id);
        return ResponseBean.success();
    }

    //入库添加时，加载来源的数据
    @GetMapping("/findAll")
    public ResponseBean<List<SupplierVO>> findAll() {
        List<SupplierVO> supplierVOList= supplierService.findAll();
        return ResponseBean.success(supplierVOList);
    }
}
